/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xui_lib.tabbar;

import com.xuexiang.xui_lib.tabbar.tablayout.jptabbar.BadgeRelativeLayout;
import com.xuexiang.xui_lib.util.Log;

import ohos.agp.animation.AnimatorProperty;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.utils.TextAlignment;
import ohos.app.Context;

/**
 * TabBarItem类
 *
 * @since 2021-04-21
 */
public class JPTabItem extends BadgeRelativeLayout {
    private static final float DEFAULT_SCALEX = 1.2f;
    private static final float DEFAULT_SCALEY = 0.3f;
    private static final String TAG = "JPTabItem";
    static final int id = 123;
    static final int redwhite = 24;
    //private Context mContext;
    private String mTitle;
    private int mIndex;

    // 对应图标的大小
    private int mIconSize;

    // 中间图标
    //private int mIddleView;

    // Tab的上下边距
    private int mMargin;
    private int mMarginTop;

    // 选中颜色(包括底部文字和图标)
    private int mSelectColor;

    // 没有选中的颜色(包括底部文字和图标)
    private int mNormalColor;

    // Tab字体大小
    private int mTextSize;

    // 是否被选中
    private boolean isSelected;

    // Tab的没有选中图标
    private int normalIcon;
    private int selectIcon;

    // 图标ImageView
    private Image mIconimg;
    private Image mIconimgs;
    private Text mText;
    private Text mText2;
    private Component redcircle;
    private boolean isRed;

    /**
     * 上下文类
     *
     * @param context
     */
    public JPTabItem(Context context) {
        super(context);
    }

    /**
     * 初始化布局和数据
     *
     * @param context 上下文
     */
    private void init(Context context) {
        mContext = context;
        DirectionalLayout.LayoutConfig config = new DirectionalLayout.LayoutConfig(
            ComponentContainer.LayoutConfig.MATCH_CONTENT, ComponentContainer.LayoutConfig.MATCH_CONTENT);
        config.weight = 1;
        setLayoutConfig(config);

        initChildView();

        addDrawTask((component, canvas) -> {
            drawText();
        });
    }

    /**
     * 初始化 文字 + 图片
     */
    private void initChildView() {
        mIconimgs = new Image(getContext());
        DependentLayout.LayoutConfig imgConfigs = new DependentLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT,
            LayoutConfig.MATCH_CONTENT);
        imgConfigs.addRule(LayoutConfig.HORIZONTAL_CENTER);
        mIconimgs.setLayoutConfig(imgConfigs);
        mIconimgs.setMarginBottom(mMargin);
        mIconimgs.setPixelMap(normalIcon);
        mIconimgs.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        mIconimgs.setWidth(mIconSize);
        mIconimgs.setHeight(mIconSize);
        addComponent(mIconimgs);

        mIconimg = new Image(getContext());
        DependentLayout.LayoutConfig imgConfig = new DependentLayout.LayoutConfig(LayoutConfig.MATCH_CONTENT,
            LayoutConfig.MATCH_CONTENT);
        mIconimg.setId(id);
        mIconimg.setLayoutConfig(imgConfig);
        mIconimg.setMarginBottom(mMargin);
        imgConfig.addRule(LayoutConfig.HORIZONTAL_CENTER);
        mIconimg.setScaleMode(Image.ScaleMode.CLIP_CENTER);
        mIconimg.setWidth(mIconSize);
        Log.d(TAG,"mIconSize=="+mIconSize);
        mIconimg.setHeight(mIconSize);
        mIconimg.setPixelMap(selectIcon);
        addComponent(mIconimg);

        mText2 = new Text(getContext());
        mText2.setText(mTitle);
        mText2.setTextSize(mTextSize);
        mText2.setTextAlignment(TextAlignment.CENTER);
        mText2.setTextColor(new Color(mNormalColor));
        mText2.setMarginTop(mMarginTop);
        addComponent(mText2);
        DependentLayout.LayoutConfig layoutConfig2 = new DependentLayout.LayoutConfig(LayoutConfig.MATCH_PARENT,
            LayoutConfig.MATCH_CONTENT);
        layoutConfig2.addRule(LayoutConfig.BELOW, id);
        mText2.setLayoutConfig(layoutConfig2);

        mText = new Text(getContext());
        mText.setText(mTitle);
        mText.setTextSize(mTextSize);
        mText.setTextColor(new Color(mNormalColor));
        mText.setTextAlignment(TextAlignment.CENTER);
        mText.setMarginTop(mMarginTop);
        addComponent(mText);
        DependentLayout.LayoutConfig layoutConfig = new DependentLayout.LayoutConfig(LayoutConfig.MATCH_PARENT,
            LayoutConfig.MATCH_CONTENT);
        layoutConfig.addRule(LayoutConfig.BELOW, id);
        mText.setLayoutConfig(layoutConfig);
        setisRed();
    }

    private void setisRed() {
        // 画气泡红点
        redcircle = new Component(getContext());
        DependentLayout.LayoutConfig circleConfig = new DependentLayout.LayoutConfig(redwhite, redwhite);
        redcircle.setLayoutConfig(circleConfig);
        ShapeElement element = new ShapeElement();
        element.setRgbColor(RgbPalette.RED);
        element.setShape(ShapeElement.OVAL);
        element.setCornerRadius(0);
        redcircle.setBackground(element);
        circleConfig.addRule(LayoutConfig.ALIGN_RIGHT, id);
        addComponent(redcircle);
    }

    /**
     * 选中变化
     */
    private void drawText() {
        // 监听红色圆点
        redcircle.setClickedListener(new ClickedListener() {
            @Override
            public void onClick(Component component) {
                redcircle.setVisibility(Component.INVISIBLE);
            }
        });
        if (isRed) {
            redcircle.setVisibility(Component.VISIBLE);
        } else {
            redcircle.setVisibility(Component.INVISIBLE);
        }
        if (isSelected) {
            // 透明度变化
            mIconimg.setAlpha(1);
            mIconimgs.setAlpha(0);
            mText.setAlpha(1);
            mText2.setAlpha(0);
            setSelectAnimate(mIconimg);
        } else {
            mIconimg.setAlpha(0);
            mIconimgs.setAlpha(1);
            mText.setAlpha(0);
            mText2.setAlpha(1);
            setNormalAnimate(mIconimg);
        }
        mText.setTextColor(isSelected ? new Color(mSelectColor) : new Color(mNormalColor));
    }

    private void setNormalAnimate(Image normaimg) {
        AnimatorProperty property = normaimg.createAnimatorProperty();
        property.scaleX(1.0f).scaleY(1.0f).start();
    }

    // 选中放大
    private void setSelectAnimate(Image selectimg) {
        AnimatorProperty property = selectimg.createAnimatorProperty();
        property.scaleX(DEFAULT_SCALEX).scaleY(DEFAULT_SCALEX).start();
    }

    /**
     * 判断是否选中
     *
     * @param isSelecteds 选中效果
     */
    public void setBarSelected(boolean isSelecteds) {
        this.isSelected = isSelecteds;
        if (isSelected) {
            isRed = false;
        }
        invalidate();
    }

    public int getIndex() {
        return mIndex;
    }

    /**
     * Builder类
     *
     * @since 2021-04-21
     */
    static class Builder {
        private int iconSize;
//        private int middleView;
        private int margin;
        private int marginTop;
        private int selectColor;
        private int normalColor;
        private int textSize;
        private int normalIcon;
        private int selectIcon;
        private String title;
        private Context context;
        private int index;
        private boolean isSelected;
        private boolean isRed;

        Builder(Context context) {
            this.context = context;
        }

        Builder setIconSize(int size) {
            this.iconSize = size;
            return this;
        }

        Builder setmiddleView(int middleview) {
//            this.middleView = middleview;
            return this;
        }

        Builder setIndex(int index) {
            this.index = index;
            return this;
        }

        Builder setNormalColor(int normalColor) {
            this.normalColor = normalColor;
            return this;
        }

        Builder setSelectedColor(int selectColors) {
            this.selectColor = selectColors;
            return this;
        }

        Builder setMargin(int margin) {
            this.margin = margin;
            return this;
        }

        Builder setMarginTop(int margintop) {
            this.marginTop = margintop;
            return this;
        }

        Builder setIsSeleted(boolean isSelecte) {
            this.isSelected = isSelecte;
            return this;
        }

        Builder setRed(boolean isReds) {
            this.isRed = isReds;
            return this;
        }

        Builder setNormalIcon(int icon) {
            this.normalIcon = icon;
            return this;
        }

        Builder setSelectIcon(int icon) {
            this.selectIcon = icon;
            return this;
        }

        Builder setTextSize(int size) {
            this.textSize = size;
            return this;
        }

        Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        JPTabItem build() {
            JPTabItem item = new JPTabItem(context);
            item.mTextSize = textSize;
            item.mTitle = title;
            item.mNormalColor = normalColor;
            item.mSelectColor = selectColor;
            item.normalIcon = normalIcon;
            item.selectIcon = selectIcon;
            item.mIndex = index;
            item.mIconSize = iconSize;
            //item.mIddleView = middleView;
            item.mMargin = margin;
            item.mMarginTop = marginTop;
            item.isSelected = isSelected;
            item.isRed = isRed;
            item.init(context);
            return item;
        }
    }
}
